//
//  PublishMenuViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/13.
//

import UIKit

class PublishMenuViewController: CommonViewController {
    let contentView  = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        let subView = UIView()
        view.addSubview(subView)
        view.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.0)
        view.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.0)
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 5
        contentView.clipsToBounds = true
        view.addSubview(contentView)
        contentView.snp.makeConstraints({
            $0.width.centerX.equalToSuperview()
            $0.top.equalTo(ScreenHeight)
        })
        
        let headView = UIView()
        headView.backgroundColor = UIColor.white
        contentView.addSubview(headView)
        headView.snp.makeConstraints({
            $0.left.top.right.equalTo(0)
        })
        if DataManager.login {
            let image = UIImageView()
            image.isUserInteractionEnabled = true
            headView.addSubview(image)
            image.backgroundColor = .red
            image.snp.makeConstraints({
                $0.left.top.equalTo(15)
                $0.height.width.equalTo(15)
                $0.bottom.equalToSuperview().inset(15)
            })
            let name = UILabel()
            name.text = "这条路有点长"
            name.font = Font_Size_14
            name.textColor = B000_COLOR
            headView.addSubview(name)
            name.snp.makeConstraints({
                $0.centerY.equalTo(image)
                $0.left.equalTo(image.snp.right).offset(10)
            })
            name.isUserInteractionEnabled = true
            image.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(userInfoTap)))
            name.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(userInfoTap)))
            
            let box = UIButton(type: .custom)
            headView.addSubview(box)
            box.setTitle("草稿箱", for: .normal)
            box.titleLabel?.font = Font_Size_14
            box.setTitleColor(B000_COLOR, for: .normal)
            box.addTarget(self, action: #selector(draftBox), for: .touchUpInside)
            box.snp.makeConstraints({
                $0.right.equalToSuperview().inset(15)
                $0.centerY.equalTo(image)
            })
            
        }else{
            let arrow = UIImageView()
            let image = UIImage(named: "arrowright")?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
            arrow.image = image
            arrow.tintColor = B000_COLOR
            let infobtn = UIButton(type: .custom)
            headView.addSubview(infobtn)
            infobtn.snp.makeConstraints({
                $0.top.left.equalTo(5)
                $0.bottom.equalToSuperview().inset(5)
            })
            infobtn.contentEdgeInsets = UIEdgeInsets.init(top: 15, left: 10, bottom: 10, right: 5)
            infobtn.setTitle("立即登录", for: .normal)
            infobtn.titleLabel?.font = Font_Size_12
            infobtn.setTitleColor(B000_COLOR, for: .normal)
            infobtn.addTarget(self, action: #selector(userInfoTap), for: .touchUpInside)
            headView.addSubview(arrow)
            arrow.snp.makeConstraints({
                $0.centerY.equalTo(infobtn)
                $0.left.equalTo(infobtn.snp.right)
            })
        }
        
        let names = ["微头条","文章","问答","视频","直播"]
        let images = ["","","","",""]
        var temp:UILabel?
        for i in 0..<names.count {
            let image = UIImageView()
            image.backgroundColor = UIColor.red
            view.addSubview(image)
           
            let label = UILabel()
            label.textAlignment = .center
            label.font = Font_Size_12
            label.textColor = B000_COLOR
            label.text = names[i]
            view.addSubview(label)
            label.snp.makeConstraints({
                if temp == nil{
                    $0.leading.equalTo(0)
                }else{
                    $0.leading.equalTo(temp!.snp.trailing)
                    $0.width.equalTo(temp!)
                }
                
                $0.top.equalTo(headView.snp.bottom).offset(60)
                if i == names.count-1{
                    $0.trailing.equalToSuperview()
                }
            })
            image.snp.makeConstraints({
                $0.width.height.equalTo(30)
                $0.bottom.equalTo(label.snp.top).offset(-5)
                $0.centerX.equalTo(label)
            })
            temp = label
        }
        let line = UIView()
        contentView.addSubview(line)
        line.backgroundColor = Separator_COLOR
        line.snp.makeConstraints({
            $0.top.equalTo((temp?.snp.bottom)!).offset(15)
            $0.right.left.equalTo(0)
            $0.height.equalTo(10)
        })
        
        let idea = ButtonArrowView()
        idea.binding(SetItemModel.init(type: .Arrow, title: "创作灵感",tip: "为你推荐热门话题"))
        contentView.addSubview(idea)
        idea.snp.makeConstraints({
            $0.left.right.equalTo(0)
            $0.top.equalTo(line.snp.bottom)
        })
        let activity = ButtonArrowView()
        activity.binding(SetItemModel.init(type: .Arrow, title: "创作活动",tip: "参与活动赢取奖励"))
        contentView.addSubview(activity)
        activity.snp.makeConstraints({
            $0.left.right.equalTo(0)
            $0.top.equalTo(idea.snp.bottom)
        })
        
        let closebtn = UIButton(type: .custom)
        contentView.addSubview(closebtn)
        closebtn.setImage(UIImage(named: "close"), for: .normal)
        closebtn.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15)
        closebtn.snp.makeConstraints({
            $0.top.equalTo(activity.snp.bottom).offset(30)
            $0.height.equalTo(40)
            $0.bottom.equalToSuperview().inset(15)
            $0.centerX.equalToSuperview()
        })
        closebtn.addTarget(self, action: #selector(dismissView), for: .touchUpInside)
        subView.snp.makeConstraints({
            $0.left.top.right.equalTo(0)
            $0.bottom.equalTo(contentView.snp.top)
        })
        subView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(dismissView)))
        contentView.addGestureRecognizer(UIPanGestureRecognizer.init(target: self, action: #selector(panView(pan:))))
        NotificationMananger.addNotification(.LoginCompletion, target: self, selector: #selector(loginCompleted))
//        self.navigationController?.modalPresentationCapturesStatusBarAppearance = true
    }
   
    @objc func panView(pan:UIPanGestureRecognizer){
        let point = pan.translation(in: view)
        if pan.state == .changed {
            if point.y>=0 {
                contentView.snp.remakeConstraints({
                    $0.width.centerX.equalToSuperview()
                    $0.bottom.equalToSuperview().offset(point.y)
                })
            }
            
        }else if pan.state == .ended {
            if  point.y>20 {
                self.dismissView()
            }else{
                contentView.snp.remakeConstraints({
                    $0.width.centerX.equalToSuperview()
                    $0.bottom.equalTo(0)
                })
            }
        }
        self.view.layoutIfNeeded()
        SystemLog(point)
    }
   
    @objc func loginCompleted(){
        
    }
    @objc func draftBox() {
        let vc = DraftBoxViewController()
        self.pushViewController(vc, animated: true)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barStyle = .black
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        contentView.snp.remakeConstraints({
            $0.width.centerX.equalToSuperview()
            $0.bottom.equalTo(0)
        })
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
            self.view.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.7)
        }
    }
    @objc func dismissView(){
        contentView.snp.remakeConstraints({
            $0.width.centerX.equalToSuperview()
            $0.top.equalTo(ScreenHeight)
        })
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
            self.view.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.0)
        }completion: { (res) in
            self.dismiss(animated: false, completion: {
                
            })
        }
    }
    @objc func userInfoTap(){
        if DataManager.login {
            let vc = ProfileInfoViewController()
            self.pushViewController(vc, animated: true)
        }else{
            let vc = LoginViewController().setNavicationBar()!
            vc.modalPresentationStyle = .fullScreen
            self.present(vc, animated: true) {

            }
        }
    }
}

